#include <QSplitter>
#include <QVBoxLayout>

#include "webinspector.h"
#include "webparser.h"
#include "downloadcenter.h"
#include "downloaditem.h"
#include "searchbar.h"

namespace Jcr
{

WebInspector::WebInspector(QWidget *parent) : QWidget(parent)
{
    init();
}

QSize WebInspector::sizeHint() const
{
    return QSize(500, 500);
}

void WebInspector::downloadUrl(const QUrl& url)
{
    m_webParser->setUrl(url);
}

void WebInspector::setDownloadCenterVisible(bool visible)
{
    m_downloadCenter->setVisible(visible);
}

void WebInspector::init()
{
    setWindowTitle(tr("Web Inspector"));
    QVBoxLayout* layout = new QVBoxLayout;
    QSplitter* splitter = new QSplitter(Qt::Vertical, this);
    layout->addWidget(splitter);
    setLayout(layout);

    QWidget* topWidget = new QWidget(this);
    QVBoxLayout* topWidgetLayout = new QVBoxLayout;
    topWidget->setLayout(topWidgetLayout);

    m_searchBar = new Jcr::SearchBar(this);
    topWidgetLayout->addWidget(m_searchBar);

    m_webParser = new Jcr::WebParser(this);
    topWidgetLayout->addWidget(m_webParser);

    m_downloadCenter = new Jcr::DownloadCenter(this);
    splitter->addWidget(topWidget);
    splitter->addWidget(m_downloadCenter);

    connect(m_webParser, SIGNAL(download(QUrl)), m_downloadCenter, SLOT(download(QUrl)));
    connect(m_searchBar, SIGNAL(parseUrl(QUrl)), m_webParser, SLOT(setUrl(QUrl)));
    connect(m_searchBar, SIGNAL(downloadUrl(QUrl)), m_downloadCenter, SLOT(download(QUrl)));
    connect(m_searchBar, SIGNAL(setDownloadCenterVisible(bool)), this, SLOT(setDownloadCenterVisible(bool)));
    connect(m_searchBar, SIGNAL(downloadSelectedItems()), m_webParser, SLOT(downloadSelectedItems()));
    connect(m_webParser, SIGNAL(downloadEnabled(bool)), m_searchBar, SLOT(onDownloadEnabled(bool)));
}

} // namespace jcr
